package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.InsuranceSettingDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.InsuranceSetting;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.List;

/**
 * @InsuranceSettingController
 * @Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/insuranceSetting")
public class InsuranceSettingController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private InsuranceSettingDao insuranceSettingDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改
     * @param insuranceSetting
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateInsuranceSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateInsuranceSetting(@RequestBody InsuranceSetting insuranceSetting){
        log.debug("添加/修改");
        insuranceSettingDao.save(insuranceSetting);
        return JsonResp.ok(insuranceSetting);
    }

    /**
     * @批量添加/修改
     * @param insuranceSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomeInsuranceSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomeInsuranceSetting(@RequestBody List<InsuranceSetting> insuranceSettings){
        log.debug("批量添加/修改");
        insuranceSettingDao.save(insuranceSettings);
        return JsonResp.ok();
    }

    /**
     * @获取详情-ByInsuranceSettingId
     * @param insuranceSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/insuranceSettingDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findInsuranceSettingDetailsByInsuranceSettingId(Long insuranceSettingId) {
        log.debug("获取详情-ByInsuranceSettingId");
        return JsonResp.ok(insuranceSettingDao.findAll());
    }

    /**
     * @分页查询(jpa方式)
     * @param insuranceSetting 分页条件
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageInsuranceSettingJpa(@RequestBody(required = false) InsuranceSetting insuranceSetting, Integer pageNum, Integer pageSize) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
        if (null == insuranceSetting) {
            page = insuranceSettingDao.findAll(pageable); //无条件分页
        }/*else{
            final String condition =  insuranceSetting.getUserName();
            page = insuranceSettingDao.findAll( 		  //条件分页
                    (root, criteriaQuery, cb) -> {
                        Predicate predicate = cb.conjunction();
                        List<Expression<Boolean>> expressions = predicate.getExpressions();
                        if (StringUtils.isNotBlank(condition)) {
                            expressions.add(cb.like(root.<String>get("condition"), "%"+ condition +"%"));
                        }
                        return predicate;
                    }, pageable);
        }*/
//        List<InsuranceSetting> content = page.getContent();
//        for (InsuranceSetting insuranceSetting1 : content) {
//        }
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除-one(永久)
     * @param insuranceSettingId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteInsuranceSettingOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteInsuranceSettingOne(Long insuranceSettingId) {
        log.debug("删除-one(永久)");
        insuranceSettingDao.delete(insuranceSettingId);
        return JsonResp.ok();
    }

    /**
     * @批量删除(永久)
     * @param insuranceSettings
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomeInsuranceSetting",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomeInsuranceSetting(@RequestBody List<InsuranceSetting> insuranceSettings) {
        log.debug("批量删除(永久)");
        insuranceSettingDao.delete(insuranceSettings);
        return JsonResp.ok();
    }


}
